package com.sense.kukoo.biz_messages.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.sense.kukoo.biz_messages.R
import com.sense.kukoo.biz_messages.adapter.MessagesItemAdapter
import com.sense.kukoo.biz_messages.databinding.BizMessagesActivityMessagesSystemsBinding
import com.sense.kukoo.biz_messages.vm.MessagesSystemsViewModel
import com.sense.kukoo.common.anim.BottomMotionAnimHelper
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.biz.MessagesItemBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.state.NetworkErrorState
import com.zy.multistatepage.state.SuccessState

@Route(path = ARouterUrl.ROUTE_MESSAGES_MODULE_MESSAGES_SYSTEMS_ACTIVITY)
class MessagesSystemsActivity : BaseMvvmActivity<MessagesSystemsViewModel, BizMessagesActivityMessagesSystemsBinding>() {

    private val TAG: String = javaClass.simpleName
    private val mAnimHelper = BottomMotionAnimHelper()

    private lateinit var mAdapter: MessagesItemAdapter
    private var type: Int = 4

    @JvmField
    @Autowired(name = ARouterUrl.KEY_FROM)
    var from: String? = null

    override fun responseVM(action: Int) {}

    override fun getLayoutId(): Int = R.layout.biz_messages_activity_messages_systems

    override fun getViewModelClass(): Class<MessagesSystemsViewModel> = MessagesSystemsViewModel::class.java

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .navigationBarColor(com.sense.kukoo.common_design.R.color.bg_background)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .fitsSystemWindows(false)
            .init()
    }

    override fun initData() {
        ARouter.getInstance().inject(this)
        type = from!!.toInt()
        mViewModel.setFromType(type)
        //
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        bindAction(mViewModel.action_back){
            mAnimHelper.animateToFinish()
        }
        bindAction(mViewModel.action_networkError){
            mBinding.multiStateContainer.show<NetworkErrorState> {
                it.onRetry {
                    mViewModel.onNetworkRetry()
                }
            }
        }
        bindAction(mViewModel.action_finishRefresh){
            mBinding.refreshLayout.finishRefresh()
        }
        bindAction(mViewModel.action_finishLoadMore){
            mBinding.refreshLayout.finishLoadMore()
        }
        bindData(mViewModel.data_enableLoadMore){
            mBinding.refreshLayout.setEnableLoadMore(it)
        }
        bindData(mViewModel.data_messages){
            mBinding.multiStateContainer.show<SuccessState>()
            mAdapter.list = it
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        //
        mAdapter = MessagesItemAdapter(arrayListOf(), mListener)
        mBinding.recyclerView.adapter = mAdapter

        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        mBinding.refreshLayout.setOnRefreshListener {
            //OnRefresh
            mViewModel.refreshSystemsData()
        }
        mBinding.refreshLayout.setOnLoadMoreListener {
            //OnLoadMore
            mViewModel.loadMoreSystemsData()
        }
        //
        when (type) {
            4 -> {
                mBinding.tvTitle.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.messages_name_system)
            }
            5 -> {
                mBinding.tvTitle.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.messages_name_creator)
            }
        }
        //弹出动画
        val first = ApplicationContext.dp2px(800F).toFloat()
        mAnimHelper.init(mBinding.flBgRoot, mBinding.clContentRoot, first){ finish() }
        mAnimHelper.animateToStart()
        //
        mBinding.refreshLayout.autoRefresh()
    }

    override fun onBackPressed() {
        mAnimHelper.animateToFinish()
    }

    private val mListener = object : MessagesItemAdapter.MessagesItemListener {
        override fun onViewMore(data: MessagesItemBean) {}

        override fun onDeleteItem(data: MessagesItemBean) {}

        override fun onClickMusic(data: MessagesItemBean) {}

        override fun onClickChannel(data: MessagesItemBean) {}

        override fun onClickFollow(data: MessagesItemBean) {}
    }
}